class MyResource:
    def query(self):
        print('query data')


from contextlib import contextmanager


@contextmanager
def make_my_resource():
    print('connect to resource')
    yield MyResource()
    print('close resource connection')


with make_my_resource() as my_resource:
    my_resource.query()
